package com.fingersoft.feature.haikangmonitor

import android.content.Context
import com.fingersoft.common.preference.UserAppPreferenceHelper
import com.fingersoft.common.preference.provider.UserIdProvider
import com.fingersoft.feature.haikangmonitor.api.IHaikangmonitorCallBack
import com.fingersoft.feature.haikangmonitor.api.IHaikangmonitorRequest
import com.fingersoft.im.BuildConfig
import com.fingersoft.im.api.UserAgent
import com.fingersoft.im.utils.AppUtils
import com.fingersoft.im.utils.JSONUtils
import com.fingersoft.theme.ThemeM
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import okhttp3.Call
import okhttp3.Response
import org.json.JSONObject
import java.lang.Exception

class SealHaikangmonitorCallBack : IHaikangmonitorCallBack {
// {
    // "msg"=>"",
    // "code"=>200,
    // "data"=>[
        // {"password"=>"321", "port"=>12345, "name"=>"测试8", "channelNum"=>0, "id"=>"test1", "uped"=>false, "url"=>"127.0.0.1", "username"=>"ceshiceshi"},
        // {"password"=>"321", "port"=>32767, "name"=>"测试1", "channelNum"=>0, "id"=>"test2", "uped"=>false, "url"=>"127.0.0.1", "username"=>"ceshiceshi"},
        // {"password"=>"321", "port"=>1234, "name"=>"测试10", "channelNum"=>0, "id"=>"test3", "uped"=>false, "url"=>"127.0.0.1", "username"=>"ceshiceshi"},
        // {"password"=>"111", "port"=>1111, "name"=>"测试", "channelNum"=>0, "id"=>"test4", "uped"=>false, "url"=>"127.0.0.1", "username"=>"test"},
        // {"password"=>"111", "port"=>8000, "name"=>"test", "channelNum"=>45, "id"=>"test5", "uped"=>false, "url"=>"127.0.0.1", "username"=>"test"}
    // ],
    // "page"=>{
        // "total"=>5,
        // "last"=>true,
        // "size"=>5,
        // "page"=>1,
        // "first"=>true
    // },
    // "timestamp"=>1522215325928
// }
    //设备搜索
    override fun searchMonitorList(context: Context, keyword: String, page: Int, callback: IHaikangmonitorRequest) {
        val params = JSONObject();
        params.put("name", keyword);
        params.put("page", page);
        params.put("size", 20);
        OkGo.post(AppUtils.getApiUrl("i/monitor/query.page", AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(UserAgent(AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did)))
                .upJson(params.toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(p0: String?, p1: Call?, p2: Response?) {
                        callback.onSuccess(JSONObject(p0))
                    }

                    override fun onError(call: Call?, response: Response?, e: Exception?) {
                        callback.onFail(e?.message)
                    }

                })
    }
    //设备置顶
    override fun upMonitor(context: Context, monitorId: String, callback: IHaikangmonitorRequest) {
        val params = JSONObject();
        params.put("id", monitorId);
        OkGo.post(AppUtils.getApiUrl("i/monitor/up", AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(UserAgent(AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did)))
                .upJson(params.toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(p0: String?, p1: Call?, p2: Response?) {
                        callback.onSuccess(JSONObject(p0))
                    }
                    override fun onError(call: Call?, response: Response?, e: Exception?) {
                        callback.onFail(e?.message)
                    }
                })
    }
    //设备取消置顶
    override fun cancelupMonitor(context: Context, monitorId: String, callback: IHaikangmonitorRequest) {
        val params = JSONObject();
        params.put("id", monitorId);
        OkGo.post(AppUtils.getApiUrl("i/monitor/cancelup", AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(UserAgent(AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did)))
                .upJson(params.toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(p0: String?, p1: Call?, p2: Response?) {
                        callback.onSuccess(JSONObject(p0))
                    }
                    override fun onError(call: Call?, response: Response?, e: Exception?) {
                        callback.onFail(e?.message)
                    }
                })
    }
    //获取设备下的摄像头列表
    override fun getMonitorCameraList(context: Context, monitorId: String, callback: IHaikangmonitorRequest) {
        val params = JSONObject();
        params.put("id", monitorId);
        OkGo.post(AppUtils.getApiUrl("i/monitor/camera/list", AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(UserAgent(AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did)))
                .upJson(params.toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(p0: String?, p1: Call?, p2: Response?) {
                        callback.onSuccess(JSONObject(p0))
                    }

                    override fun onError(call: Call?, response: Response?, e: Exception?) {
                        callback.onFail(e?.message)
                    }

                })
    }
    //收藏摄像头
    override fun favorateCamera(context: Context, cameraIds: List<String>, callback: IHaikangmonitorRequest) {
        val params = JSONObject();
        var gson = Gson();
        params.put("ids", gson.toJson(cameraIds));
        OkGo.post(AppUtils.getApiUrl("i/monitor/camera/favorate", AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(UserAgent(AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did)))
                .upJson(params.toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(p0: String?, p1: Call?, p2: Response?) {
                        callback.onSuccess(JSONObject(p0))
                    }
                    override fun onError(call: Call?, response: Response?, e: Exception?) {
                        callback.onFail(e?.message)
                    }
                })
    }
    //摄像头取消收藏
    override fun unfavorateCamera(context: Context, cameraIds: List<String>, callback: IHaikangmonitorRequest) {
        val params = JSONObject();
        var gson = Gson();
        params.put("ids", gson.toJson(cameraIds));
        OkGo.post(AppUtils.getApiUrl("i/monitor/camera/unfavorate", AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(UserAgent(AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did)))
                .upJson(params.toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(p0: String?, p1: Call?, p2: Response?) {
                        callback.onSuccess(JSONObject(p0))
                    }
                    override fun onError(call: Call?, response: Response?, e: Exception?) {
                        callback.onFail(e?.message)
                    }
                })
    }
    //获取设备列表
    override fun getMonitorList(context: Context, page: Int, callback: IHaikangmonitorRequest) {
        searchMonitorList(context,"", page, callback);
    }

    override fun useTheme(): Boolean {
        return AppUtils.useTheme()
    }

    override fun getThemeColor(): Int {
        return ThemeM.themeColor
    }

    override fun getUserAppPreferenceHelper(): UserAppPreferenceHelper {
        return UserAppPreferenceHelper(object : UserIdProvider() {
            override fun getUserId(): String {
                return AppUtils.getUser().userId ?: ""
            }
        })
    }
}
